from typing import *


class Solution:
    def countGood(self, nums: List[int], k: int) -> int:

        cnt = Counter()
        ans = 0
        tot = 0
        l = 0
        for v in nums:
            tot += cnt[v]
            cnt[v] += 1
            while l < len(nums) and tot - cnt[nums[l]] + 1 >= k:
                cnt[nums[l]] -= 1
                tot -= cnt[nums[l]]
                l += 1
            if tot >= k:
                ans += l + 1
        return ans
